const app = getApp()
const util = require('../../../../../../utils/util.js');
const time = require('../../../../../../utils/time.js');
const api = require('../../../../../../config/api.js');
Page({
  data: {
    circleid: "",
    auxiliary: { "searchText": '', "showFunctionPop": false, "showFunctionPopIndex": ""},
    //主题组信息
    themeList: [],
  },
  onLoad: function (options) {
    var _this = this;
    _this.setData({
      type: options.type,
      circleid: options.circleid,
    })
  },
  /**
   * 搜索框输入
   */
  inputSearchText: function(e){
    var _this = this
    var value = e.detail.value.replace(/\s+/g, '')
    _this.setData({
      ["auxiliary.searchText"]: value
    })
  },
  /**
   * 搜索
   */
  searchTheme: function(){
    var _this = this;
    var keyword = _this.data.auxiliary.searchText;
    var circleid = _this.data.circleid;
    if (String(keyword).length > 0) {
      var data = {};
      data.circleid = circleid;
      data.keyword = keyword;
      var openid = app.globalData.logininfo.openid;
      if (openid != null && openid != "") {
        util.post(api.fetchToken()).then(function (res) {
          if (res.access_token != '') {
            return Promise.resolve({
              access_token: res.access_token
            });
          }
        }).then(function (res) {
          data.openid = openid;
          data.access_token = res.access_token;
          _this.searchThemeQuery(api.Interest_SearchThemeApi, data);
        })
      }else{
        _this.searchThemeQuery(api.Interest_SearchTheme, data);
      }
    }else{
      _this.setData({
        themeList: []
      })
    }
  },
  /**
   * 搜索，请求
   */
  searchThemeQuery: function(url, data){
    var _this = this;
    util.post(url, data).then(function (res) {
      if (res.code == 200) {
        _this.setData({
          themeList: res.result
        })
      } else {
        util.showToast(res.message, "none");
      }
    })
  },

  /**
   * 打开功能面板
   */
  openFunctionPop: function (e) {
    var _this = this;
    var index = e.currentTarget.dataset.index;
    this.setData({
      ["auxiliary.showFunctionPop"]: true,
      ["auxiliary.showFunctionPopIndex"]: index,
    })
  },
  /**
   * 关闭所有弹窗
   */
  closeMC: function () {
    var _this = this;
    _this.setData({
      ["auxiliary.showFunctionPop"]: false,
      ["auxiliary.showFunctionPopIndex"]: ""
    })
  },
  /**
   * 主题设置页
   */
  navigateToThemeSet: function () {
    var _this = this;
    var index = _this.data.auxiliary.showFunctionPopIndex;
    var themeid = _this.data.themeList[index].id;
    var circleid = _this.data.circleid;
    wx.navigateTo({
      url: '../../makeTheme/themeSet/themeSet?circleid=' + circleid + '&themeid=' + themeid,
    })
  },

  /**
   * 进入主题编辑页面
   */
  navigateToMakeTheme: function (e) {
    var _this = this;
    var circleid = _this.data.circleid;
    var index = e.currentTarget.dataset.index;
    if (index != null && String(index).length > 0) {
      var copy = e.currentTarget.dataset.copy;
      var themeid = _this.data.themeList[index].id;
      if (copy == "copy") {
        wx.navigateTo({
          url: '../../makeTheme/makeTheme?o_type=copy&themeid=' + themeid + '&circleid=' + circleid + '&type=' + _this.data.type,
        })
      } else {
        wx.navigateTo({
          url: '../../makeTheme/makeTheme?o_type=update&themeid=' + themeid + '&circleid=' + circleid + '&type=' + _this.data.type,
        })
      }
    } else {
      wx.navigateTo({
        url: '../../makeTheme/makeTheme?o_type=create&themeid=&circleid=' + circleid + '&type=' + _this.data.type,
      })
    }
  }, 

  /**
   * 删除主题
   */
  deleteTheme: function () {
    var _this = this;
    var index = _this.data.auxiliary.showFunctionPopIndex;
    var themeid = _this.data.themeList[index].id;
    var openid = app.globalData.logininfo.openid;
    if (openid != null && openid != "") {
      util.post(api.fetchToken()).then(function (res) {
        if (res.access_token != '') {
          return Promise.resolve({
            access_token: res.access_token
          });
        }
      }).then(function (res) {
        var data = {};
        data.themeid = themeid;
        data.openid = openid;
        data.access_token = res.access_token;
        util.post(api.Interest_DeleteThemeApi, data).then(function (res) {
          if (res.code == 200) {
            _this.closeMC();
            util.showToast("删除成功", "success");
            _this.searchTheme();
          } else {
            util.showToast(res.message, "none");
          }
        })
      })
    }
  }
})